#ifndef XM_THREAD_HH
#define XM_THREAD_HH

#include <functional>
#include <thread>
#include <memory>

#ifdef __linux__
#include <unistd.h>
#endif

#include <string>
#include <atomic>

#include "XM_noncopyable.hh"
#include "pch.hh"

XM_NAMESPACE_BEGIN

struct Thread : private XM_noncopyable
{
    using ThreadFunc = std::function<void()>;

    explicit Thread(ThreadFunc dofunc, const std::string& name = str::string());
    ~Thread();

    void start();
    void join();

    bool isStarted() { return started__; }

    pid_t tid() const { return tid__; }

    const std::string& name() const { return name__; }

    static int numCreated() { return numCreated__; }

private:
    void setDefaultName();

    bool started__;
    bool joined__;
    std::shared_ptr<std::thread> thread__;
    pid_t tid__;
    ThreadFunc func__;
    std::string name__;
    static std::atomic_int numCreated__; 
};

XM_NAMESPACE_END

#endif // header file protect